// 环境健康检测系统
#include<stdio.h>
#include<string.h>
#define MAX_RECORDS 10

//健康监测记录结构体
typedef struct {
	float temperature;
	float humidity;
	char advice[256];
} HealthRecord;

HealthRecord records[MAX_RECORDS];
int recordCount =0;

//检测温度并返回建议
void checkTemperature(float temperature, char *advice) {
	if (temperature > 35) {
		printf("温度过高，请注意防暑！\n");
		sprintf(advice,"温度过高，请注意防暑！");
	} else if (temperature <10) {
		printf("温度过低，请注意保暖！\n");
		sprintf(advice,"温度过低，请注意保暖！");
	} else {
		printf("温度适宜。\n");
		sprintf(advice,"温度适宜。");
	}
}

//检测湿度并返回建议
void checkHumidity(float humidity, char *advice) {
	if (humidity> 70) {
		printf("湿度较高，请注意防潮！\n");
		sprintf(advice,"湿度较高，请注意防潮！");
	} else if (humidity <30) {
		printf("湿度较低，请注意防潮！\n");
		sprintf(advice,"湿度过低，请注意防潮！");
	} else {
		printf("温度适宜。\n");
		sprintf(advice,"温度适宜。");
	}
}

//记录环境数据

void recordData(float temperature,float humidity, char *advice) {
	if (recordCount <MAX_RECORDS) {
		records[recordCount].temperature =temperature;
		records[recordCount]. humidity=humidity;
		strcpy(records[recordCount].advice,advice);
		recordCount++;
	} else {
		printf("记录已满，无法保存更多数据。\n");
	}
}

//显示历史记录
void viewRecords() {
	if(recordCount == 0) {
		printf("当前没有任何健康监测记录。\n");
		return;
	}
	
	printf("\n健康监测历史记录:\n");
	for (int i = 0; i < recordCount; i++) {
		printf("记录 %d:温度 %.1f℃，湿度 %.1f%%,建议：%s\n",
			i + 1, records[i].temperature, records[i].humidity);
	}
}

//统计数据
void calculateSummary() {
	if (recordCount == 0) {
		printf("当前没有任何记录，无法统计。\n");
		return;
	}
	float totalTemp = 0, totalHumidity = 0;
	for (int i = 0; i < recordCount; i++) {
		totalTemp +=records[i].temperature;
		totalHumidity += records[i].humidity;
	}
	printf("\n数据统计 ：\n");
	printf("记录总数 ：%d 条\n",recordCount);
	printf("平均温度 ：%.1f℃\n",totalTemp / recordCount);
	printf("平均湿度 ：%.1f%%\n",totalHumidity /recordCount);
}
int main() {
	//显示操作菜单
	while(1) {
		printf("\n====环境健康监测系统====\n");
		printf("1.检测当前环境\n");
		printf("2.查看历史记录\n");
		printf("3.查看数据统计\n");
		printf("4.退出\n");
		printf("请选择操作:\n");//定义一个整数
		
		int choice;
		scanf("%d",&choice);
		
		if (choice == 1) {
			float temperature, humidity;
			char advice[100] ="";
			
			printf("\n请输入当前温度(℃) ：");
			scanf("%f",&temperature);
			printf("请输入当前湿度(%) ：");
			scanf("%f",&humidity);
			
			printf("\n检测结果 ：\n");
			checkTemperature(temperature, advice);
			checkHumidity(humidity, advice);
			
			recordData(temperature, humidity, advice);
		} else if (choice == 2) {
			viewRecords();
		} else if (choice == 3) {
			calculateSummary();
		} else if (choice == 4) {
			printf("感谢使用环境健康监测系统！再见！\n");
			break;
		} else {
			printf("无效选择，请重新输入。\n");
		}
	}
	return 0;
}
